// pages/index/index.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:'/static/images/no-pic.png',
    no_cate:'/static/images/empty/home_cate.png',
    current: 0,
    city:'hide',
    hotSearch: [],
    card:[],
    list:[],
    category: [],
    slide: [],
    fastac:{show:0,list:[]},
    banner_center:{img:''},
    banner_bottom:{img:''},
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,
    opacity: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showShareMenu({
      withShareTicket:true,
      menus:["shareAppMessage","shareTimeline"]
    })
  },
  onShow() {
    let sf=wx.getStorageSync('sf') || '';
    //判断用户身份
    if(sf=='worker'){
      wx.redirectTo({
        url:"/pages/workerCode/home"
      })
      return;
    }

    let city=wx.getStorageSync("city");
    if(city){
      this.setData({
        city:city
      });
    }
    this.loadCity();
    this.load();
  },
  async loadCity(){
    if(this.data.city!="hide"){
      return;
    }
    let res=await wx.util.post("index/city");
    this.setData({
      city:res.data.city
    });
  },
  toCard(e){
    let id=e.currentTarget.dataset.id;
    if(id==0){
      wx.util.href('/pages/card/card');
      return;
    }
    wx.util.href('/pages/card/detail?id='+id);
  },
    async load(){
      let res=await wx.util.post("index/index",{city:this.data.city});
      this.setData({
        card:res.data.card,
        slide:res.data.slide,
        category:res.data.category,
        list:res.data.list,
        banner_bottom:res.data.banner_bottom,
        banner_center:res.data.banner_center,
        hotSearch:res.data.keyword,
        fastac:res.data.fastac
      });
      wx.setNavigationBarTitle({ title: res.data.title || '首页' });
    },
    adAction(e){
      let s=e.currentTarget.dataset.s;
      wx.util.bannerAc(s);
    },
    toCate(e){
      let item=e.currentTarget.dataset.item;
      wx.navigateTo({
        url:"/pages/classify/cate?fid="+item.id+"&n="+item.name
      })
    },
    toCity(){
      wx.navigateTo({
        url:"/pages/selectCity?currentCity="+this.data.city
      })
    },
    searchKey(e){
      let item=e.currentTarget.dataset.item;
      wx.navigateTo({
        url:"/pages/list/list?searchKey="+item
      })
    },
    detail: function(e) {
      let id=e.currentTarget.dataset.id;
      if(id==0){
        return;
      }
      wx.navigateTo({
        url: '/pages/productDetail/productDetail?id='+id
      });
    },
    search: function() {
      wx.navigateTo({
        url: '/pages/search/search'
      });
    },

  onShareTimeline: function (e) {
    let wxid=wx.getStorageSync("openid") || '';
    return {
      query: 'wxid='+wxid,
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let wxid=wx.getStorageSync("openid") || '';
    return {
      path: '/pages/index/index?wxid='+wxid,
    }
  }
})